﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UIWindow.Enums;
using UIWindow.Lucifer;

namespace UIWindow.Protocols.C_Mode
{
    /// <summary>
    /// 响应数据
    /// </summary>
    public class ResponseFrameFormat : IProtocol
    {
        /// <summary>
        /// 单元号
        /// </summary>
        public ushort UnitNumber { get; set; }
        /// <summary>
        /// 命令
        /// </summary>
        public OmronHeaderCodeType HeaderCode { get; set; }
        /// <summary>
        /// 数据
        /// </summary>
        public List<int> DataList = new List<int>();
        /// <summary>
        /// 响应码
        /// </summary>
        public ushort EndCode { get; set; }

        public ResponseFrameFormat()
        {

        }

        public byte[] GetStream()
        {
            return new byte[0];
        }

        public IProtocol Handle(byte[] bytes)
        {
            string str = Encoding.ASCII.GetString(bytes);
            UnitNumber = ushort.Parse(str.Substring(1, 2));
            HeaderCode = (OmronHeaderCodeType)Enum.Parse(typeof(OmronHeaderCodeType), str.Substring(3, 2), true);
            EndCode = ushort.Parse(str.Substring(5, 2));
            //str = str.Substring(7, str.Length - 11);
            str = str[7..^4];
            if ((int)HeaderCode < 9)
            {
                int count = str.Length / 4;
                for (int i = 0; i < count; i++)
                {
                    DataList.Add(int.Parse(str.Substring(i * 4, 4)));
                }
            }
            return this;
        }
    }
}
